package com.example.demo.service.impl;

import com.example.demo.dao.ProductDetailDAO;
import com.example.demo.dataobject.ProductDetailDO;
import com.example.demo.model.ProductDetail;
import com.example.demo.service.ProductDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class ProductDetailServiceImpl implements ProductDetailService {

    @Autowired
    private ProductDetailDAO productDetailDAO;

    @Override
    public int save(ProductDetail productDetail) {
        if (productDetail.getId() == null) {
            productDetail.setId(UUID.randomUUID().toString().replace("-", ""));
        }
        if (productDetailDAO.selectById(productDetail.getId()) == null) {
            return productDetailDAO.insert(productDetail.toDO());
        }else {
            return productDetailDAO.updateById(productDetail.toDO());
        }
    }

    @Override
    public List<ProductDetail> getByProductId(String productId) {
        List<ProductDetailDO> productDetailDOS=productDetailDAO.selectByProductId(productId);
        List<ProductDetail> productDetails=new ArrayList<ProductDetail>();
        productDetailDOS.forEach(productDetailDO->{productDetails.add(productDetailDO.toModel());});
        return productDetails;
    }

    @Override
    public ProductDetail get(String id) {
        return productDetailDAO.selectById(id).toModel();
    }
}
